package nuist.hanniko.stu.webbackend.dao.mapper;

import nuist.hanniko.stu.webbackend.model.Notification;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public interface NotificationMapper {

    @Insert("insert into notification(principal, content, confirmed, create_time)" +
            "values (#{principal}, #{content}, #{confirmed}, #{createTime})")
    void insert(Notification notification);

    @Update("update notification set confirmed=1 where _id=#{id}")
    int confirm(String id);

    @Update({
            "<script>",
            "<foreach collection='idCollection' item='item' separator=';'>",
            "update notification ",
            "<set>",
            "confirmed=1",
            "</set>",
            "where _id=#{item}",
            "</foreach>",
            "</script>"
    })
    int confirmBatch(@Param("idCollection") List<String> idCollection);

    @Select("select * from notification where principal = #{uid}")
    @Results({
            @Result(property = "id", column = "_id"),
            @Result(property = "principal", column = "principal"),
            @Result(property = "content", column = "content"),
            @Result(property = "confirmed", column = "confirmed"),
            @Result(property = "createTime", column = "create_time")
    })
    List<Notification> getAllByUid(String uid);

    @Select("select * from notification where principal=#{uid} and confirmed=0")
    List<Notification> getAllUnconfirmedByUid(String uid);
}
